﻿using System;
using System.ComponentModel.Composition;
using Cubicle.Core.Interfaces;

namespace Cubicle.Core.Processors.Parameter
{
    [Export("Cubicle.Processors.Parameter.NullParameterResolver", typeof(IProcessor<ParameterRequest>))]
    [ExportMetadata("Name", "NullParameterResolver")]
    [ExportMetadata("Description", "Recognizes null parameters.")]
    public class NullParameterResolver : ConditionalProcessor<ParameterRequest>
    {

        public const string NullString = "#null#";

        #region Overrides of Processor<ParameterRequest>

        public override bool Condition(ParameterRequest request)
        {
            return request.Source == null || (request.Source is string && request.Source.ToString().Equals(NullString));
        }

        public override ParameterRequest ConditionalProcess(ParameterRequest request)
        {
            request.Complete(Convert.ChangeType(null, request.TargetType));
            return request;
        }

        #endregion
    }
}